package com.wuhenzhizao.sku.utils;

import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * 数字展示工具类
 *
 * @author wuhenzhizao
 * @since 2017-08-02
 */

public class NumberUtils {
    private NumberUtils() {
    }

    /**
     * double转int
     *
     * @param value
     * @return String
     */
    public static String formatNumber(double value) {
        return String.format("%.2f", value);
    }

    /**
     * int转String
     *
     * @param value
     * @param pattern
     * @return String
     */
    public static String formatNumber(Object value, String pattern) {
        if (value != null) {
            return new DecimalFormat(pattern).format(value);
        }
        return "";
    }

    /**
     * 返回整数格式
     *
     * @param value
     * @param pattern
     * @return int
     */
    public static int formatNumberReturnInteger(Object value, String pattern) {
        if (value != null) {
            return Integer.valueOf(formatNumber(value, pattern));
        }
        return 0;
    }

    /**
     * 返回浮点数格式
     *
     * @param value
     * @param pattern
     * @return double
     */
    public static double formatNumberReturnDouble(Object value, String pattern) {
        if (value != null) {
            DecimalFormat format = new DecimalFormat(pattern);
            format.setRoundingMode(RoundingMode.HALF_UP);
            return Double.valueOf(format.format(value));
        }
        return 0.0;
    }
}
